package u1;

import static java.lang.System.*;
import static u1.Util.*;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Takes titles list on standard in. Takes one argument: the name of a file
 * containing page numbers (non-negative integers). Outputs the list of titles
 * corresponding to those page numbers.
 */
public class NumberTitles {
  public static void main(String[] args) throws IOException {
    if (args.length != 1) {
      err.println("See comments for usage.");
      exit(-1);
    }
    File lookupFile = new File(args[0]);

    /*
     * Read titles into memory because it's easy.
     */
    BufferedReader titlesReader = new BufferedReader(new InputStreamReader(in,
        "UTF-8"));
    List<String> titles = new ArrayList<String>(PAGES_MAX);
    readLines(titles, titlesReader);
    titlesReader.close();
    trace("Read %d titles.", titles.size());
    
    /*
     * Map to ordinals from lookup file.
     */
    List<Integer> lookup = new ArrayList<Integer>();
    readIntegers(lookup, new FileInputStream(lookupFile));
    for (Integer i : lookup) {
      if (i < 0 || i > titles.size()) {
        trace("Invalid page number %d.", i);
      } else {
        out.println(titles.get(i));
      }
    }
  }
}
/*
* Copyright (c) 2009 John Lees-Miller
* 
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
* 
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

